package com.lepu.app.fun.mother_circle.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.application.MyApplication;
import com.core.lib.utils.main.DateUtilBase;
import com.eyzhs.app.R;
import com.lepu.app.fun.my.bean.BeanPubContent;
import com.lepu.app.usercenter.bean.LoginInfo;

public class AdapterMotherFun extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	@SuppressWarnings("unused")
	private Context mContext = null;
	private ArrayList<BeanPubContent> mArrayList = null;

	public AdapterMotherFun(Context context, ArrayList<BeanPubContent> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.mother_fun_listitem, null);

			holder = new ViewHolder();
			
			RelativeLayout mainItemLayout = (RelativeLayout) convertView.findViewById(R.id.mainItemLayout);
			TextView titleTextView = (TextView) convertView.findViewById(R.id.titleTextView);
			TextView nameTextView = (TextView) convertView.findViewById(R.id.nameTextView);
			TextView countTextView = (TextView) convertView.findViewById(R.id.countTextView);
			TextView pubTimeTextView = (TextView) convertView.findViewById(R.id.pubTimeTextView);
			LinearLayout deleteTopicLayout = (LinearLayout) convertView.findViewById(R.id.deleteTopicLayout);
			
			ImageView motherFunTopJianImageView = (ImageView) convertView.findViewById(R.id.motherFunTopJianImageView);
			
			holder.mainItemLayout = mainItemLayout;
			holder.titleTextView = titleTextView;
			holder.nameTextView = nameTextView;
			holder.countTextView = countTextView;
			holder.pubTimeTextView = pubTimeTextView;
			holder.deleteTopicLayout = deleteTopicLayout;
			holder.motherFunTopJianImageView = motherFunTopJianImageView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		final BeanPubContent item = mArrayList.get(position);
		
		holder.titleTextView.setText(item.TopicTitle);
		holder.nameTextView.setText(item.UserNickName);
		holder.countTextView.setText(item.ViewCount+"阅读");
		holder.pubTimeTextView.setText(DateUtilBase.friendly_time(item.RecordCreateTime));
		
		if(MyApplication.getInstance().checkIsLogin())
		{
			LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
			if(item.UserID.equals(loginInfo.UserID))
			{
				holder.deleteTopicLayout.setVisibility(View.VISIBLE);
				holder.deleteTopicLayout.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						//FragmentMotherFun.getInstance().deleteItem(position);
					}
				});
			}else{
				holder.deleteTopicLayout.setVisibility(View.INVISIBLE);
			}
		}else{
			holder.deleteTopicLayout.setVisibility(View.INVISIBLE);
		}
		
		if(item.IsGood == 1)
		{
			holder.motherFunTopJianImageView.setVisibility(View.VISIBLE);
		}else{
			holder.motherFunTopJianImageView.setVisibility(View.GONE);
		}
		
		return convertView;
	}

	private static class ViewHolder {
		@SuppressWarnings("unused")
		RelativeLayout mainItemLayout;
		TextView titleTextView;
		TextView nameTextView;
		TextView countTextView;
		TextView pubTimeTextView;
		LinearLayout deleteTopicLayout;
		
		ImageView motherFunTopJianImageView;
	}
}
